package com.gobo.jinghuimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.gobo.jinghuimall.product.entity.AttrEntity;
import com.gobo.jinghuimall.product.vo.AttrGroupRelationVo;
import com.gobo.jinghuimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gobo.jinghuimall.product.entity.AttrGroupEntity;
import com.gobo.jinghuimall.product.service.AttrGroupService;
import com.gobo.common.utils.PageUtils;
import com.gobo.common.utils.R;



/**
 * 属性分组
 *
 * @author guanbo
 * @email gobostart@163.com
 * @date 2021-10-16 20:46:46
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private com.gobo.jinghuimall.product.service.AttrService attrService;

    //product/attrgroup/{attrgroupId}/attr/relation
    //获取指定分组关联的所有属性
    @GetMapping("/{attrgroupId}/attr/relation")
    public R listAttrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntityList = attrGroupService.getAttrRelation(attrgroupId);

        return R.ok().put("data", attrEntityList);
    }
    ///product/attrgroup/attr/relation

    /**
     * 添加属性与分组关联关系
     * @param attrGroupRelationVo
     * @return
     */
    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVo){
        attrGroupService.saveAttrRelation(attrGroupRelationVo);

        return R.ok();
    }

    /**
     * 删除属性 属性分组的关联关系
     * @param attrGroupRelationVo
     * @return
     */
    ///product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVo){
        attrGroupService.deleteAttrRelation(attrGroupRelationVo);

        return R.ok();
    }

    /**
     * /product/attrgroup/{catelogId}/withattr
     * 获取分类下所有分组&关联属性
     *
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        return R.ok().put("data",attrGroupWithAttrsVos);
    }

    /**
     * 获取属性分组没有关联的其他属性
     * @return
     */
    ///product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R querynoAttrRelationAttr(@PathVariable("attrgroupId") Long attrgroupId,@RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoAttrRelationAttr(attrgroupId,params);

        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }
    @GetMapping("list/{catelogId}")
    public R listGroup(@PathVariable("catelogId") Long catelogId,@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPageByCatelogId(params,catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getByIdAndCatelogPath(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
